home *** CD-ROM | disk | FTP | other *** search
/ Games of Daze / Infomagic - Games of Daze (Summer 1995) (Disc 1 of 2).iso / x2ftp / msdos / vla / viewtga / viewtga.asm < prev    next >
Assembly Source File  |  1994-01-14  |  8KB  |  258 lines

  1. ┌────────────────────────────────────────────────────────────────────────────
  2.     IDEAL
  3.     MODEL SMALL
  4.     STACK 200h
  5.     p386
  6.     ASSUME CS:@CODE, DS:@DATA
  7.     JUMPS
  8. ┌────────────────────────────────────────────────────────────────────────────
  9.  DATASEG
  10. └────────────────────────────────────────────────────────────────────────────
  11.  
  12. VGASeg      dw  0a000h              ;Vga Segment in High Memmory
  13.  
  14. XSize       dw  ?                   ;Picture X and Y
  15. YSize       dw  ?
  16.  
  17. FileHandle  dw  ?                   ;Save the file handle in case we need it
  18.  
  19. Header      db  18 DUP(0)           ;Buffer for header
  20.  
  21. TmpPal      db  768 DUP(0)          ;We will load the TGA palette here 
  22.                                     ; before we swap it to RGB
  23. Palette     db  768 DUP(0)          ;Space for the final palette
  24.  
  25. Credits     db  "TGA Viewer by VLA",10,13,"$"
  26. FileError   db  "File Not Found ",10,13,"$"
  27.  
  28. ┌────────────────────────────────────────────────────────────────────────────
  29.  CODESEG
  30. └────────────────────────────────────────────────────────────────────────────
  31.  
  32. ; External procedure from CLSUB.OBJ
  33.  
  34.  EXTRN   GetCommandLine:PROC
  35.  
  36.     ┌────────────────────────────────────────────────
  37.     ; File:     CLSUB.ASM
  38.     ;
  39.     ; ┌────────────────────────────────────────────────
  40.     ; Proc:     GetCommandLine
  41.     ;
  42.     ;       Gets a filename off the command line and stores
  43.     ;  it at [cs:dx], and adds the 5 byte extension at [cs:bx]
  44.     ;
  45.     ;  Ex.   C:\> viewtga vlagold2
  46.     ;  Ex.
  47.     ;  Ex.   cs:dx  -> FileName     db 13 DUP (' ') 
  48.     ;  Ex.   cs:bx  -> Extension    db ".tga",0
  49.     ;  Ex.
  50.     ;  Ex.   cs:dx  -> FileName     db "vlagold2.tga",0
  51.     ;
  52.     ; Entry: 
  53.     ;
  54.     ;   ES      = PSP SEG  (ES points to this at entry of your program )
  55.     ;                      (otherwise use Int 21h Service 51h to get it)
  56.     ;   CS:DX   = pointer to filename area
  57.     ;   CS:BX   = pointer to 5 byte 0 terminating Extension to add
  58.     ;
  59.     ; Return: 
  60.     ;
  61.     ;   AX      = length of command line
  62.     ; └────────────────────────────────────────────────
  63.     ;
  64.     └────────────────────────────────────────────────
  65.  
  66. ;These are needed in the CodeSeg by GetCommandLine
  67.  
  68. FileName    db  13 DUP(' ')
  69. Extension   db  ".tga",0
  70.  
  71.     ┌─────────────────────────────────
  72.     ;PROC ClearVGA
  73.     ;
  74.     ;   Move Palette Color 0 into all offsets on the screen
  75.     ;
  76.     ;Entry:     None
  77.     ;Return:    None
  78.     └─────────────────────────────────
  79. PROC ClearVGA
  80.     push    ax cx di es             ;Save the registers we will use
  81.  
  82.     mov     es,[VGASeg]             ;Move the segment to the VGA seg
  83.     xor     ax,ax                   ;Put 0's in both ah and al
  84.     xor     di,di                   ;Start at offset 0
  85.     mov     cx,1000h/2              ;1000h bytes in VGA seg /2 = words
  86.     rep     lodsw                   ;load cx words in ax to [es:di]
  87.  
  88.     pop     es di cx ax             ;Restore our registers
  89.     ret         
  90. ENDP ClearVGA
  91.  
  92.     ────────────────────────────────────────────────────────────────────
  93.     ; Write a specified palette chunk
  94.     ────
  95.     ; IN: DS:SI = Address of palette
  96.     ;        CX = Number of colors to write (NOT *3)
  97.     ;        AL = Starting palette index
  98.     ;
  99.     ;OUT: None
  100.     ────────────────────────────────────────────────────────────────────
  101. PROC WritePalette
  102.     push    dx cx si
  103.     mov     dx,cx                   ;CX * 3, but faster than using MUL or IMUL
  104.     add     cx,cx
  105.     add     cx,dx
  106.     mov     dx,03c8h                ;Hardware port for write palette
  107.     out     dx,al                   ;Starting at
  108.     inc     dx                      ;Next port writes it in
  109.     cld                             ;Move up through ds:si
  110.     rep outsb                       ;Store byte from ds:si to port dx
  111.     pop     si cx dx
  112.     ret
  113. ENDP
  114.  
  115. ┌─────────────────────────────────
  116.  
  117. Start:
  118.     mov     ax,@Data                ;Set ds to DataSeg
  119.     mov     ds,ax
  120.  
  121.     mov     ax,13h                  ;320x200x256 mode
  122.     int     10h
  123.     
  124.     mov     si,offset TmpPal        ;Clear the Palette
  125.     mov     cx,256
  126.     mov     al,0
  127.     call    WritePalette
  128.  
  129.     call    ClearVGA                ;Clear the Screen
  130.     
  131.     mov     dx,offset FileName      ;cs:dx filename
  132.     mov     bx,offset Extension     ;cs:bx extension
  133.     call    GetCommandLine          ;Get the FileName
  134.  
  135.     cmp     ax,1
  136.     jle     @@FileError
  137.  
  138.     mov     ax,cs                   ;filename is in the cs
  139.     mov     ds,ax
  140.     mov     dx,offset FileName      ;ds:dx filename
  141.     mov     ah,3dh
  142.     mov     al,11000010b            ;Attribute (Normal)
  143.     int     21h                     ;Load the File
  144.     jc      @@FileError
  145.  
  146.     mov     bx,ax
  147.     mov     ax,@Data                ;Switch back to dataseg
  148.     mov     es,ax
  149.     mov     ds,ax
  150.     mov     [FileHandle],bx         ;Save the handle
  151.     
  152.     mov     dx,offset Header
  153.     mov     cx,18
  154.     
  155.     mov     ah,3fh                  ;Load in the 18 byte TGA Header
  156.     int     21h
  157.     
  158.     mov     si,offset Header
  159.     add     si,12                   ;12th byte is XSize
  160.     mov     ax,[ds:si]
  161.     mov     [XSize],ax
  162.     
  163.     add     si,2                    ;14th byte is YSize
  164.     mov     ax,[ds:si]
  165.     mov     [YSize],ax
  166.  
  167.     mov     cx,768                  ;256 Colors, 3 bytes per color
  168.     mov     dx,offset TmpPal        ;   This is TGA Palette BGR 
  169.     mov     ah,3fh                  ;   needs to be swapped to RGB
  170.     int     21h
  171.     jc      @@FileError
  172.  
  173.     
  174.     mov     ax,[XSize]              ;Get picture size from X * Y
  175.     mov     cx,[YSize]
  176.     mul     cx
  177.     mov     cx,ax                   ;Load that many bytes
  178.     mov     ds,[VGASeg]             ;Directly to the VGASeg
  179.     mov     dx,0
  180.     mov     ah,3fh
  181.     int     21h
  182.     jc      @@FileError
  183.  
  184.     mov     ax,@Data                ;Ready to Swap the palette
  185.     mov     ds,ax
  186.     mov     si,offset TmpPal
  187.     mov     di,offset Palette
  188.     mov     cx,256                  ;256 colors
  189.  
  190. @@PaletteFlip:
  191.     mov     al,[ds:si]              ; We load the Blue into al
  192.     inc     si
  193.     mov     ah,[ds:si]              ; Green to ah
  194.     inc     si
  195.     mov     bl,[ds:si]              ; Red to bl
  196.     inc     si
  197.  
  198.     shr     al,2                    ;TGA palette is multiplied by 4
  199.     shr     ah,2                    ;shr al,2 divides al by 4
  200.     shr     bl,2                    ;do this to all the colors
  201.     
  202.     mov     [es:di],bl              ;Flip the order back to RGB
  203.     inc     di
  204.     mov     [es:di],ah              ;So we can load it directly to the
  205.     inc     di
  206.     mov     [es:di],al              ;VGA Palette
  207.     inc     di
  208.  
  209.     loop    @@PaletteFlip           ;Loop until we do all the colors
  210.  
  211.     mov     si,offset Palette       ;Info needed to load the palette
  212.     mov     cx,256
  213.     mov     al,0
  214.  
  215.     call    WritePalette            ;Move it to the VGA Palette
  216.     
  217.     mov     ah,0                    ;Wait for Key Press
  218.     int     16h
  219.  
  220.     mov     ah,1                    ;Grab the Key
  221.     int     16h
  222.  
  223. @@Done:
  224.     mov     bx,0                    ;Switch back to text mode
  225.     mov     ah,0                    ;first video page
  226.     mov     al,3
  227.     int     10h    
  228.     
  229.     mov     dx,offset Credits       ;Print the credits
  230.     mov     ah,9
  231.     int     21h
  232.  
  233.     mov     bx,[FileHandle]         ;Close the file
  234.     mov     ah,3eh
  235.     int     21h
  236.     
  237.     jmp     @@Bye
  238.  
  239. @@FileError:
  240.     mov     ax,@Data
  241.     mov     ds,ax
  242.  
  243.     mov     bx,0                    ;Switch to text mode
  244.     mov     ah,0
  245.     mov     al,3
  246.     int     10h    
  247.     
  248.     mov     dx,offset FileError     ;Tell the user it did find it
  249.     mov     ah,9                    ; (most likely)
  250.     int     21h
  251.  
  252. @@Bye:
  253.     mov     ah,4ch                  ;See ya...
  254.     int     21h
  255. END Start
  256.  
  257. └────────────────────────────────────────────────────────────────────────────
  258.